package office.service.workflow.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.workflow.form.RemindConditionForm;
import office.action.workflow.form.RemindForm;
import office.action.workflow.wrapper.RemindWrapper;
import office.bo.workflow.impl.Remind;
import office.dao.workflow.RemindDP;
import office.domainset.BizDomainProviderSetImpl;
import office.service.workflow.IRemindService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class RemindServiceImpl extends BaseBizServiceImpl implements IRemindService {

	public void haveReadRemind(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Remind bo = (Remind)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getRemindDP(), id);
			bo.setHandleState(1);
			bo.store(domainProviderSetImpl);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("对提醒进行已阅失败!", e);
		}
	}
	
	public void editRemind(RemindForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Remind bo = (Remind)domainProviderSetImpl.getRemindDP().newEmptyObj();
			bo =(Remind) form.copyPropertiesToBO(bo);
			//构建查询是否存在的条件
			RemindConditionForm rcf=new RemindConditionForm();
			rcf.setHandleState(0);
			rcf.setObjId(bo.getObjId());
			rcf.setRemindUser(bo.getRemindUser());
			rcf.setObjClassName(bo.getObjClassName());
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			if(this.findRemindByCondition(rcf,null).size()<1){
				bo.store(domainProviderSet);
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑消息提醒失败!", e);
		}
	}
	public RemindWrapper getById(String id)throws BusinessException{
		RemindWrapper form = new RemindWrapper();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Remind bo = (Remind)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getRemindDP(), id);
			form.copyToWrapper(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取消息提醒失败!", e);
		}
		return form;
	}
	@SuppressWarnings("unchecked")
	public List<RemindWrapper> findRemindByCondition(RemindConditionForm condition,Page page)throws BusinessException{
		List<RemindWrapper> wrapperList = new ArrayList<RemindWrapper>();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getRemindDP();
			Map<String,Object> condMap= new HashMap<String,Object>();
			if(null!=condition){
				if(!Util.isNull(condition.getId())){
					condMap.put("id", condition.getId());
				}
				if(!Util.isNull(condition.getName())){
					condMap.put("name", condition.getId());
				}
				if(!Util.isNull(condition.getObjId())){
					condMap.put("objId", condition.getObjId());
				}
				if(!Util.isNull(condition.getObjClassName())){
					condMap.put("objClassName", condition.getObjClassName());
				}
				if(!Util.isNull(condition.getSendDate())){
					condMap.put("sendDate", condition.getSendDate());
				}
				if(!Util.isNull(condition.getRemindUser())){
					condMap.put("remindUser", condition.getRemindUser());
				}
				if(!Util.isNull(condition.getLookDate())){
					condMap.put("lookDate", condition.getLookDate());
				}
				if(!Util.isNull(condition.getHandleState())){
					condMap.put("handleState", condition.getHandleState());
				}
			}
			List<Remind> list = null;
			if(page == null){
				list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findRemindByCondition",condMap,new HashMap());
			}else{
				list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findRemindByCondition",condMap,new HashMap(),page);
			}
			RemindWrapper wrapper;
			for(int i=0;i<list.size();i++){
				Remind bo = (Remind)list.get(i);
				wrapper = new RemindWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取消息提醒列表失败！", e);
		}
		return wrapperList;
	}
	
	@SuppressWarnings("unchecked")
	public Long findRemindCountByType(RemindConditionForm condition)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getRemindDP();
			Map<String,Object> condMap= new HashMap<String,Object>();
			if(null!=condition){
				if(!Util.isNull(condition.getId())){
					condMap.put("id", condition.getId());
				}
				if(!Util.isNull(condition.getName())){
					condMap.put("name", condition.getId());
				}
				if(!Util.isNull(condition.getObjId())){
					condMap.put("objId", condition.getObjId());
				}
				if(!Util.isNull(condition.getObjClassName())){
					condMap.put("objClassName", condition.getObjClassName());
				}
				if(!Util.isNull(condition.getSendDate())){
					condMap.put("sendDate", condition.getSendDate());
				}
				if(!Util.isNull(condition.getRemindUser())){
					condMap.put("remindUser", condition.getRemindUser());
				}
				if(!Util.isNull(condition.getLookDate())){
					condMap.put("lookDate", condition.getLookDate());
				}
				if(!Util.isNull(condition.getHandleState())){
					condMap.put("handleState", condition.getHandleState());
				}
			}
			
			List<Long> list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findRemindCountByCondition",condMap,new HashMap());
			if(list != null){
				return list.get(0);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取消息提醒列表失败！", e);
		}
		return 0l;
	}

	@Override
	public Page findRemindByConditionForPage(RemindConditionForm condition)
			throws BusinessException {
		Page page = new Page();
		int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "common.pageSize"));
		page.setPageSize(pageSize);
		Long l = findRemindCountByType(condition);
		if(l%pageSize == 0){
			page.setTotalPage(Integer.valueOf(l/pageSize+""));
		}else{
			page.setTotalPage(Integer.valueOf(l/pageSize+1+""));
		}
		return page;
	}

	@Override
	public void deleteRemind(RemindConditionForm condition) {
		Map<String,Object> condMap= new HashMap<String,Object>();
		condMap.put("handleState", condition != null ?condition.getHandleState():null);
		condMap.put("remindUser", condition != null ?condition.getRemindUser():null);
		try {
			getDP(RemindDP.class).exeDynamicHql("deleteRemind", condMap,new HashMap<String,Object>());
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@Override
	public void updateAllRemindToRead(RemindConditionForm condition) {
		Map<String,Object> condMap= new HashMap<String,Object>();
		condMap.put("handleState", condition != null ?condition.getHandleState():null);
		condMap.put("remindUser", condition != null ?condition.getRemindUser():null);
		try {
			getDP(RemindDP.class).exeDynamicHql("updateAllRemindToRead", condMap,new HashMap<String,Object>());
		} catch (Exception e) {
			log.error("", e);
		}
	}

}
